package com.example.music.Mine;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.icu.text.UnicodeSet;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


import com.example.music.LoginActivity;
import com.example.music.MainActivity;
import com.example.music.R;
import com.example.music.RegisterActivity;
import com.example.music.base.BaseFragment;
import com.example.music.sql.DBOpenHelper;

import butterknife.BindView;
import butterknife.OnClick;

public class MineFragment extends BaseFragment {
    @BindView(R.id.mine_name)
    TextView mine_name;
    @BindView(R.id.mine_login)
    TextView mine_login;
    @BindView(R.id.mine_logout)
    TextView mine_logout;
    private SharedPreferences sp;


    @OnClick({R.id.mine_login,R.id.mine_logout})
    void onClick(View v){
        switch (v.getId()){
            case R.id.mine_login:
                LoginActivity.launch(getContext());
                break;
            case R.id.mine_logout:
                logout();
                break;
        }
    }

    private void logout() {
        sp= getContext().getSharedPreferences("loginInfo",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.clear();
        editor.commit();
        initView();
        Toast.makeText(this.getContext(),"注销成功",Toast.LENGTH_SHORT).show();
    }


    @Override
    protected View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       return inflater.inflate(R.layout.fragment_mine,container,false);

    }


    @Override
    protected void initData(@Nullable Bundle savedInstanceState) {
        initView();
    }

    private void initView() {
        SharedPreferences sp= getContext().getSharedPreferences("loginInfo",Context.MODE_PRIVATE);
        if(TextUtils.isEmpty(sp.getString("username",""))){
            mine_login.setVisibility(View.VISIBLE);
            mine_logout.setVisibility(View.GONE);

        }else {
            mine_login.setVisibility(View.GONE);
            mine_logout.setVisibility(View.VISIBLE);
        }
        mine_name.setText(sp.getString("username",""));

    }

    public static MineFragment newInstance(){
        MineFragment fragment=new MineFragment();
        return fragment;
    }
}
